<?php
include  dirname(__FILE__).'/../DBConnection.php';
require_once dirname(__FILE__).'/../log4php/Logger.php';
Logger :: configure(dirname(__FILE__).'/../log4php.properties');

class tj_fuelChange{
	 public function getTjFuelChange($startDate,$stopDate,$devices,$forExcel=false){
		//	参数： $forExcel = true:  按excel导出要求决定输出数组的索引方式
		//	$startDate = date("Y-m-d H:i:s", strtotime($startDate . '-8hour'));
		//	$stopDate= date("Y-m-d H:i:s", strtotime($stopDate . '-8hour'));
		
		$data = array();
		$devices =str_replace(',', '\',\'', $devices);
		$devices=' \''.$devices.'\'';
		$sql="SELECT vm.ModelName,c.name as customer,d.deviceID,d.d_esn,vi.licenseNumber,fuel_level_bef, fuel_level_now" . 
				" FROM obd_demo.LocationHistory l,VehModelNumber vn,VehModel vm,Devices_MT dm,VehicleInfo vi, provision_obd.Devices d  ,Customers c   " .
				" where  l.deviceID=dm.deviceID " .
				" and l.deviceID=d.deviceID" .
				" and dm.ModelNumID=vn.ModelNumID".
				" and vn.ModelID=vm.ModelID".
				" and dm.customerID=c.id".
				" and  dm.vin=vi.vin and l.deviceID in ("  . $devices . ")" .
				" and gpsStamp >= '" . $startDate . "' and gpsStamp < '" . $stopDate. "' " .
				" and code=3019 ". 
				" order by l.deviceID, gpsStamp"; 
		//echo $sql . "<BR>";
	
		//$logger->debug("----------------sql:------" . $sql);
		mysql_select_db("IOV_demo");
		$result = mysql_query($sql);
		$numRows = mysql_num_rows($result);
//		$ret = array ();
//		$data= array ();
		$i =0;	//number of rows in table (not in db)
		
		if ($numRows > 0) {
			$lastDeviceID = "";	
			$totalFuelCharge=0;
			$totalFuelLoss=0;	
	
	//sample data record:
	//	dev01 licence01  fuel_level_bef01 fuel_level_now01
	//	dev01 licence01  fuel_level_bef02 fuel_level_now02
	//	dev02 licence02  fuel_level_bef03 fuel_level_now03
	//	dev02 licence02  fuel_level_bef04 fuel_level_now04
	
			while($row = mysql_fetch_object($result)){
				$fuelChange = $row->fuel_level_now - $row->fuel_level_bef;
				if ($fuelChange>=0){ //分别统计加油和漏油
					$totalFuelCharge += $fuelChange;
				}else{
					if ($fuelChange >= 25) $totalFuelLoss -= $fuelChange;
				}
				if ($row->deviceID != $lastDeviceID){	// 同一deviceID第一行记录 
					if ($forExcel == false){	
						//$data[$i] = array();
						$data[$i]["deviceID"] = $row->deviceID;
						$data[$i]["d_esn"] = $row->d_esn;
						$data[$i]["licenseNumber"] = $row->licenseNumber;
						$data[$i]["customer"] = $row->customer;
						$data[$i]["modelName"] = $row->ModelName;
						$data[$i]["totalFuelCharge"] = "-";
						$data[$i]["totalFuelLoss"] = "-";			
								
						if ($i > 0){	//上一个deviceID的统计值 
							$data[$i-1]["totalFuelCharge"] = $totalFuelCharge . "%";
							$data[$i-1]["totalFuelLoss"] = $totalFuelLoss . "%";
							$totalFuelCharge=0;
							$totalFuelLoss=0;					
						}							
					}else{	
						//$data[$i] = array();
						$data[$i][0] = $row->deviceID;
						$data[$i][1] = $row->d_esn;
						$data[$i][2] = $row->licenseNumber;
						$data[$i][3] = $row->customer;
						$data[$i][4] = $row->ModelName;
						$data[$i][5] = "-";
						$data[$i][6] = "-";			
								
						if ($i > 0){	//上一个deviceID的统计值 
							$data[$i-1]["5"] = $totalFuelCharge . "%";
							$data[$i-1]["6"] = $totalFuelLoss . "%";
							$totalFuelCharge=0;
							$totalFuelLoss=0;					
						}							
						
					}
					
					$i++;
				}					
				$lastDeviceID = $row->deviceID;		
			}
			if ($forExcel == false){				
				$data[$i-1]["totalFuelCharge"] = $totalFuelCharge . "%";	//最后一个device填写统计值 
				$data[$i-1]["totalFuelLoss"] = $totalFuelLoss . "%";
			}else{
				$data[$i-1]["5"] = $totalFuelCharge . "%";	//最后一个device填写统计值 
				$data[$i-1]["6"] = $totalFuelLoss . "%";
			}		
		}
//		$ret['total'] = $i;
//		$ret['rows'] = $data;
//		//echo json_encode($ret);
		//echo $data[0]["totalFuelLoss"];	
		return $data;		
	}
}
?>